package com.film.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.ShowingsDao;
import com.film.domain.FilmShowings;
import com.film.rowMapper.FilmShowingsRowMapper;
import com.film.rowMapper.ShowingsDateRowMapper;
import com.film.rowMapper.ShowingsRowMapper;
import com.film.util.DBUtil;

public class ShowingsDaoImpl implements ShowingsDao {
	
	private JdbcTemplate template = new JdbcTemplate(DBUtil.getDataScorse());

	@Override
	public List<FilmShowings> selectAllShowings(int showingsStatusCode) {
		String sql = "select * from film_showings t1 " + 
						"inner join film t2 " + 
						"inner join film_office t3 " + 
						"on t1.film_id = t2.film_id " + 
						"and t1.off_id = t3.off_id " + 
						"where t1.is_delete = ?;";
		return template.query(sql, new ShowingsRowMapper(),showingsStatusCode);
	}

	@Override
	public List<FilmShowings> selectShowingsByFilmId(int filmId,int showingsStatusCode) {
		String sql = "select * from film_showings t1 " + 
						"inner join film t2 " + 
						"inner join film_office t3 " + 
						"on t1.film_id = t2.film_id " + 
						"and t1.off_id = t3.off_id " + 
						"and t1.is_delete = ? " + 
						"and t2.film_id = ? " + 
						"order by t1.showings_day";
		return template.query(sql, new ShowingsRowMapper(),showingsStatusCode,filmId);
	}

	@Override
	public List<FilmShowings> selectShowingsByDate(String date, int filmId, int showingsStatusCode) {
		String sql = "select * from film_showings t1 " + 
						"inner join film t2 " + 
						"inner join film_office t3 " + 
						"on t1.film_id = t2.film_id " + 
						"and t1.off_id = t3.off_id " + 
						"and t1.is_delete = ? " + 
						"and t2.film_id = ? " + 
						"and t1.showings_day = ?";
		return template.query(sql, new ShowingsRowMapper(),showingsStatusCode,filmId,date);
	}

	@Override
	public List<String> selectShowingsDateByFilm(int filmId, int showingsStatusCode) {
		String sql = "select showings_day from film_showings "
						+ "where film_id = ? "
						+ "and is_delete = ? "
						+ "group by showings_day "
						+ "order by showings_day asc";
		return template.query(sql, new ShowingsDateRowMapper(),filmId,showingsStatusCode);
	}

	@Override
	public List<FilmShowings> selectShowingsById(int showingsId, int showingsStatusCode) {
		String sql = "select * from film_showings t1 " + 
				"inner join film t2 " + 
				"inner join film_office t3 " + 
				"on t1.film_id = t2.film_id " + 
				"and t1.off_id = t3.off_id " + 
				"and t1.showings_id = ? " + 
				"and t1.is_delete = ?";
		return template.query(sql, new ShowingsRowMapper(),showingsId,showingsStatusCode);
	}

	@Override
	public List<String> selectShowingsAll() {
		String sql="select * from film_showings";
		 
		List<String> lists=new ArrayList<>();
		lists= template.query(sql, new ShowingsDateRowMapper());
		return lists;
	}

	
	

	@Override
	public int addShowings(FilmShowings showings) {
		String sql = "insert into film_showings values(default,?,?,?,?,?,?,now(),now(),?)";
		return template.update(sql,showings.getFilm().getFilmId(),
				showings.getShowingsDay(),showings.getShowingsStartTime(),
				showings.getShowingsEndTime(),showings.getFilmOffice().getOffId(),
				showings.getShowingsPrice(),showings.getIsDelete());
	}

	@Override
	public List<FilmShowings> selectFilmShowingsByOfficAndDate(int OfficeId, String showingsDate) {
		String sql = "select * from film_showings where off_id = ? and showings_day = ?";
		return template.query(sql, new FilmShowingsRowMapper(),OfficeId,showingsDate);
	}

	
	
	
	
	
	
	@Override
	public int deleteFilmShowings(int showingsId) {
		String sql = "delete from film_showings where showings_id = ?";
		return template.update(sql,showingsId);
	}

	@Override
	public List<FilmShowings> limitShowings(int pageNum, int stripNum) {
		String sql = "select * from film_showings t1 " + 
				"inner join film t2 " + 
				"inner join film_office t3 " + 
				"on t1.film_id = t2.film_id " + 
				"and t1.off_id = t3.off_id " + 
				"where t1.is_delete = 0 limit ?,?";
		return template.query(sql, new ShowingsRowMapper(),pageNum,stripNum);
	}


}
